package com.i8office.android.zhihuixinwu.adapter;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.cyan.tools.base.BaseViewHolder;
import com.cyan.tools.base.RecyclerArrayAdapter;
import com.cyan.tools.util.RxBus;
import com.cyan.tools.widget.ItemDecoration;
import com.i8office.android.zhihuixinwu.R;
import com.i8office.android.zhihuixinwu.bean.Mode;
import com.i8office.android.zhihuixinwu.bean.People;
import com.i8office.android.zhihuixinwu.bean.Send;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by pc on 2018/7/8.
 */

public class SendAdapter extends RecyclerArrayAdapter<Send> {
    private boolean isEnd = false;
    public SendAdapter(Context context) {
        super(context);
    }

    public void setEnd(boolean end) {
        isEnd = end;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new ViewHolder(parent);
    }


    class ViewHolder extends BaseViewHolder<Send> implements PeopleAdapter.OnDelectPeople {
        @BindView(R.id.name)
        TextView name;
        @BindView(R.id.add)
        ImageButton add;
        @BindView(R.id.handlerList)
        RecyclerView handlerList;

        public ViewHolder(ViewGroup parent) {
            super(parent, R.layout.item_send);
            ButterKnife.bind(this, itemView);
        }

        @Override
        public void onDelete() {
            notifyDataSetChanged();
        }

        @Override
        public void setData(final Send send) {
            name.setText(send.getNode().getNodeName());
            PeopleAdapter peopleAdapter = null;
            if (handlerList.getAdapter() != null) {
                peopleAdapter = (PeopleAdapter) handlerList.getAdapter();
                peopleAdapter.getData().clear();
                peopleAdapter.addAll(send.getChooseList());
            } else {
                peopleAdapter = new PeopleAdapter(getContext(), send, this);
                peopleAdapter.setHas_delete(true);
                peopleAdapter.setIs_single(send.getNode().getMultiHandleMode() == 0);
                handlerList.setLayoutManager(new LinearLayoutManager(getContext()));
                handlerList.addItemDecoration(new ItemDecoration(getContext(), 1));
                handlerList.setAdapter(peopleAdapter);
                peopleAdapter.addAll(send.getChooseList());
            }
            add.setVisibility(isEnd?View.GONE:View.VISIBLE);
            add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RxBus.get().post("add_staff", new Mode(send
                            .getNode().getMultiHandleMode(), getAdapterPosition()));
                }
            });
        }
    }
}
